﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dukous.Dao.NHibernate;
using Dukous.Security.Domain;
using NHibernate;
using NHibernate.Criterion;

namespace Dukous.Security.Dao.NHibernate
{
    public class LogonInfoDao : EntityDao<LogonInfo, long>, ILogonInfoDao
    {
        #region ILogonInfoDao 成员

        public LogonInfo Find(Guid guid, string ip)
        {
            IList<LogonInfo> users = this.HibernateTemplate.ExecuteFind<LogonInfo>(delegate(ISession session)
            {
                ICriteria criteria = session.CreateCriteria(typeof(LogonInfo), "info");
                criteria.Add(Expression.Eq("info.Guid", guid));
                criteria.Add(Expression.Eq("info.IpAddress", ip));
                return criteria.SetFetchSize(1).List<LogonInfo>();
            });

            LogonInfo logonInfo =  users.FirstOrDefault();
            if (logonInfo == null)
            {
                return logonInfo;
            }

            if (logonInfo.ExpireTime < DateTime.Now)
            {
                return null;    
            }

            return logonInfo;
        }

        #endregion
    }
}
